//
//  SettingViewController.m
//  quanminzhekou
//
//  Created by zxl on 15/1/7.
//  Copyright (c) 2015年 zhongqinglongtu. All rights reserved.
//

#import "SettingViewController.h"
#import "CollectViewController.h"
#import "FeedbackViewController.h"
#import "StatementViewController.h"
#import "SDImageCache.h"
#import "VersionViewModel.h"

@interface SettingViewController ()
@property (nonatomic) NSArray* titleArr;
@property (nonatomic) UILabel* delLab;
@property (nonatomic) NSString* serviceVersion;
@property (nonatomic) NSString* updateUrlStr;
@end

@implementation SettingViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    _titleArr = @[@"消息通知", @"我的收藏", @"意见反馈", @"关于我们", @"清除缓存"];
    
    _serviceVersion = [[NSUserDefaults standardUserDefaults] objectForKey:@"Version_Service"];
    _updateUrlStr = [[NSUserDefaults standardUserDefaults] objectForKey:@"Version_URL"];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark -
- (void)showImgCacheSpace {
    CGFloat cacheSize =  [SDImageCache sharedImageCache].getSize / 1024.0 / 1024.0;
    self.delLab.text = [NSString stringWithFormat:@"%.1fM", cacheSize];
}

- (void)onSwitchValueChanged:(UISwitch*)onOff {
    [[NSUserDefaults standardUserDefaults] setBool:onOff.on forKey:@"Remote_Notification_OnOff"];
    [[NSUserDefaults standardUserDefaults] synchronize];
    
    [[NSNotificationCenter defaultCenter] postNotificationName:@"NotificationForRemote" object:nil];
}

#pragma mark - IBAction
- (IBAction)onTapBack:(id)sender {
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark - UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.titleArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"SettingCell"];
    
    if (0 == indexPath.row) {
        cell.accessoryType = UITableViewCellAccessoryNone;
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        
        BOOL isRemoteNotificationOnOff = [[NSUserDefaults standardUserDefaults] boolForKey:@"Remote_Notification_OnOff"];
        
        UISwitch* onOff = [[UISwitch alloc] initWithFrame:CGRectMake(CGRectGetWidth(self.view.bounds) - 15 - 51, (tableView.rowHeight - 31) / 2.0, 51, 31)];
        [onOff addTarget:self action:@selector(onSwitchValueChanged:) forControlEvents:UIControlEventValueChanged];
        onOff.on = isRemoteNotificationOnOff;
        [cell addSubview:onOff];
    } else if (4 == indexPath.row) {
        
        if (nil == _delLab) {
            _delLab = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetWidth(self.view.bounds) - 50 - 60, (tableView.rowHeight - 16) / 2.0, 60, 16)];
            _delLab.backgroundColor = [UIColor clearColor];
            _delLab.font = [UIFont systemFontOfSize:16];
            _delLab.textColor = [UIColor colorWithRed:0x64 / 255.0 green:0x6b / 255.0 blue:0x76 / 255.0 alpha:1.0];
            [cell addSubview:_delLab];
        }
        [self showImgCacheSpace];
    }
    
    NSString* string = [NSString stringWithFormat:@"setting%zd", (long)indexPath.row];
    cell.imageView.image = [UIImage imageNamed:string];
    cell.textLabel.text = [self.titleArr objectAtIndex:indexPath.row];
    cell.textLabel.font = [UIFont systemFontOfSize:16];
    cell.textLabel.textColor = [UIColor colorWithRed:0x64 / 255.0 green:0x6b / 255.0 blue:0x76 / 255.0 alpha:1.0];
    
    UIView* line = [[UIView alloc] initWithFrame:CGRectMake(0, tableView.rowHeight - 1, CGRectGetWidth(self.view.bounds), 1)];
    line.backgroundColor = [UIColor colorWithWhite:.97 alpha:1.0];
    [cell addSubview:line];
    
    return cell;
}

#pragma mark - UITableViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    if (1 == indexPath.row) {
        CollectViewController* controller = [self.storyboard instantiateViewControllerWithIdentifier:@"CollectViewController"];
        [self.navigationController pushViewController:controller animated:YES];
    } else if (2 == indexPath.row) {
        FeedbackViewController* controller = [self.storyboard instantiateViewControllerWithIdentifier:@"FeedbackViewController"];
        [self.navigationController pushViewController:controller animated:YES];
    } else if (3 == indexPath.row) {
        StatementViewController* controller = [self.storyboard instantiateViewControllerWithIdentifier:@"StatementViewController"];
        [self.navigationController pushViewController:controller animated:YES];
    } else if (4 == indexPath.row) {
        __weak typeof(self) weakSelf = self;
        [[SDImageCache sharedImageCache] clearDiskOnCompletion:^{
            [weakSelf showImgCacheSpace];
        }];
    }
}

@end
